package com.zl.learning.script;

import org.junit.Test;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class TestJavaScript {

    private ScriptEngine getJavascriptEngin() {
        ScriptEngineManager manager = new ScriptEngineManager();
        return manager.getEngineByName("JavaScript");
    }

    @Test
    public void greet() throws ScriptException {
        ScriptEngine engine = getJavascriptEngin();
        engine.eval("print('HelloWolrd!');");
    }

    @Test
    public void useDefaultBinding() throws ScriptException {
        ScriptEngine engine = getJavascriptEngin();
        engine.put("name","Jim");
        engine.eval("var message = 'Hello'+name;");
        engine.eval("print(message);");
        Object obj = engine.get("message");
        System.out.println(obj);
    }
}
